const app = getApp()

import { sendCode, img } from "../../utils/http";
const behavior = require('../relation_behavior')
import { HiddenCall } from '../../request/api.js'
const request = new HiddenCall
import { throttle } from '../../utils/throttle.js'
import { upload_api } from "../../common/api/api"
Component({
  /**
*  房源详情
* - @author           狄兆璐
* - @date             2018-07-11
*  model               房源详情Model
*  favoriteList        收藏夹列表
*/
  behaviors: [behavior],
  properties: {
    model: Object,
    favoriteList: Array,
    lastImagelist: Array
  },
  data: {
    imgModel: []
  },
  methods: {
    /**
     * 进入房源详情媒体列表
     * teantType          房源标志
     * model                房源详情model
     */
    enterPath: throttle(function () {
      // 跳转将详情model带入，以及房源标志
      wx.navigateTo({
        url: `../../pages/path_detail/index?data=${JSON.stringify({ teantType: "house", model: this.data.model })}`
      })
    }, 2000),
    /**
     * 进入上传图片列表
     * @author wl
     */
    enterImage() {
      this.setData({ imgModel: []})
      // 调用图片函数
      img().then(res => {
        wx.showLoading({
          icon: 'loading'
        })
        // 获取图片地址
        let list = res.tempFilePaths.map(item => {
          // 将图片地址进行上传
          return request.uploadImage(item).then(res => {
            // 正则替换
            let reg = /^(wxfile\:\/\/|http\:\/\/tmp\/)/ig
            let img_name = item.replace(reg, "")
            // 拼接图片地址上传阿里云
            return `${upload_api[0].url}/houseHiddenCall/${img_name}`
          })
        })
        // 将图片数组循环
        Promise.all(list).then(res => {
          for (let i = 0; i < res.length; i++) {
            // 放入上传图片model
            this.data.imgModel.push(res[i])
          }
          // 关闭上传类型弹窗，数据更新
          this.setData({
            imgModel: this.data.imgModel
          })
          wx.hideLoading()
          setTimeout(() => {
            wx.navigateTo({
              url: `../../pages/upload_image/index?data=${JSON.stringify({ code: this.data.model.houseCode, imgModel: this.data.imgModel })}`
            })
          }, 500)
        })
      })
    }
  }
})